package com.betterwood.dragon.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import org.jsoup.Jsoup;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class CodeUtil {
    public static String decode(byte[] inputBytes) {
        // File imageFile = new File("/Users/helldomi/Documents/jfcode.png");
        // if (!imageFile.exists()) {
        // System.out.println("pic not exist");
        // ;
        // }
        // InputStream in = null;
        // byte[] imageByte = null;
        // // 读取图片字节数组
        // try {
        // in = new FileInputStream(imageFile);
        // imageByte = new byte[in.available()];
        // in.read(imageByte);
        // in.close();
        // } catch (IOException e) {
        // e.printStackTrace();
        // }

        String imageStr = Base64.encode(inputBytes);

        String username = "dominic0312";
        // 密码
        String password = "wqxyy1985";
        // 备注字段: 可以不写
        String remark = "输出计算结果";

        Map<String, String> data = new HashMap<>();
        data.put("username", username);
        data.put("password", password);
        data.put("typeid", "1");
        data.put("remark", remark);
        // data.put("image",
        // "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAgAFoDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ip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iGRkbkJ3dGAx0OODjIFAGHc+G/iBaxiWX4nN5WcO40C3xGP7zfN09T2zk4AJHdW9nIdIt7PUbj7ZOsSLNcbPLMsgAzIAv3CWG4Y6Hp0rn73xr4flytn438OW6lMZe5ikYH1B8wD04INQ6R4z8OW1rElz4r8OhWjDNEmoRDyH/ALi/Ocr+PGOOCAoB1FtDc28hjaZZbYD5C+fMT2J539+eDwM7iSa+KvHf/JQ/Ev8A2Fbr/wBGtX17J438GSxtHJ4o0F0cFWVtQhIIPUEbq+SvFmvXn/CZa5/Z2rT/AGH+0Lj7P9nuD5fl+Y23Zg424xjHGKAP/9k=");

        data.put("image", imageStr);
        String resultString = "";
        String result = "init";

        try {
            resultString = Jsoup.connect("http://api.ttshitu.com/predict").requestBody(JSONUtil.toJsonStr(data))
                    .header("Content-Type", "application/json").ignoreContentType(true).timeout(120000).post().text();
        } catch (IOException e) {

            e.printStackTrace();
        }
        JSONObject jsonObject = JSONUtil.parseObj(resultString);
        if (jsonObject.getBool("success")) {
            result = jsonObject.getJSONObject("data").getStr("result");
            System.out.println("识别成功结果为:" + result);
        } else {
            System.out.println("识别失败原因为:" + jsonObject.getStr("message"));
        }

        return result;
    }

}
